package com.qingmeng.outsourcing.portal.service.impl;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.qingmeng.outsourcing.mapper.*;
import com.qingmeng.outsourcing.model.*;
import com.qingmeng.outsourcing.portal.enums.ProjectScheduleCode;
import com.qingmeng.outsourcing.portal.model.dto.ProjectPublishParam;
import com.qingmeng.outsourcing.portal.model.vo.ProjectListVo;
import com.qingmeng.outsourcing.portal.service.ProjectService;
import com.qingmeng.outsourcing.portal.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ProjectServiceImpl implements ProjectService {
    //项目表类型操作
    @Autowired
    private ProjectMapper projectMapper;
    //项目与工程师类型操作
    @Autowired
    private ProjectPositionTypeRelationMapper projectPositionTypeRelationMapper;
    //项目类型id
    @Autowired
    private ProjectTypeRelationMapper projectTypeRelationMapper;
    //项目进度id
    @Autowired
    private ProjectScheduleRelationMapper projectScheduleRelationMapper;
    @Autowired
    private ProjectFilesMapper projectFilesMapper;
    //项目类型关联操作mapper
    @Autowired
    private ProjectTypeMapper projectTypeMapper;
    //职位类型关联操作mapper
    @Autowired PositionTypeMapper positionTypeMapper;
    @Autowired
    private ScheduleMapper scheduleMapper;



    /**
     *
     * @param projectPublishParam 要发布的项目信息
     * @return 发布成功返回1，发布失败返回0，网络其他问题，返回-1
     */
    @Transactional
    @Override
    public int publishTack(ProjectPublishParam projectPublishParam) {
        int result=0;
        //先获取Project的数据
        Project project=new Project();
        //项目名称
        project.setProjiectName(projectPublishParam.getProjectName());
        //项目简介
        project.setProjectShortIntro(projectPublishParam.getProjectShortIntro());
        //项目预算
        project.setProjectBudget(projectPublishParam.getProjectBudget());
        //项目周期
        project.setProjectDuration(projectPublishParam.getProjectDuration());
        //项目介绍
        project.setProjectIntroduction(projectPublishParam.getProjectIntroduction());
        //工作类型
        project.setWorkType(projectPublishParam.getWorkType());
        project.setWorkExplain(project.getWorkExplain());
        //先project表中插入一条项目数据
        result=projectMapper.insertSelective(project);
        if (result<=0){
            return  0;
        }
        //获取前端传递过来的项目所需工程师类型ID列表
        List<Long> engineerTypeList=projectPublishParam.getEngineerTypeList();
        if (engineerTypeList!=null&&engineerTypeList.size()>0){
            for (int i=0;i<engineerTypeList.size();i++){
                ProjectPositionTypeRelation projectPositionTypeRelation=new ProjectPositionTypeRelation();
                projectPositionTypeRelation.setProjectId(project.getId());
                projectPositionTypeRelation.setPostitionTypeId(engineerTypeList.get(i));
                if (projectPositionTypeRelationMapper.insertSelective(projectPositionTypeRelation)<=0){
                    return 0;
                }

        }
        }
        //获取前端传递过来的项目类型id
        List<Long> projectTypeIdList=projectPublishParam.getProjectTypeIdList();
        if (projectTypeIdList!=null&&projectTypeIdList.size()>0){
            for (int i=0;i<projectTypeIdList.size();i++){
                ProjectTypeRelation projectTypeRelation=new ProjectTypeRelation();
                projectTypeRelation.setProjectId(project.getId());
                projectTypeRelation.setProjectTypeId(projectTypeIdList.get(i));
                if (projectTypeRelationMapper.insertSelective(projectTypeRelation)<=0){
                    return 0;
                }

            }
        }
//向数据库对应的表里插入项目速度信息
        ProjectScheduleRelation projectScheduleRelation=new ProjectScheduleRelation();
        projectScheduleRelation.setProjectId(project.getId());
        projectScheduleRelation.setScheduleId(ProjectScheduleCode.PUBLISHED);
        if(projectScheduleRelationMapper.insertSelective(projectScheduleRelation)<=0){
        return 0;
        }

        //获取前端传过来的项目文件列表
        List<ProjectFiles> filesList=projectPublishParam.getFilesList();
        if (filesList!=null&&!filesList.isEmpty()){
            for (int i=0;i<filesList.size();i++){
            ProjectFiles projectFiles=new ProjectFiles();
            projectFiles.setProjectId(project.getId());
            projectFiles.setFileName(filesList.get(i).getFileName());
            projectFiles.setFileUrl(filesList.get(i).getFileUrl());
         /*   projectFiles.setProjectId(2L);*/
            projectFiles.setFileTypeId(filesList.get(i).getFileTypeId());
            projectFiles.setCreateTime(new Date());
            projectFiles.setUserId(SecurityUtils.getCurrentUser().getId());
            if (projectFilesMapper.insertSelective(projectFiles)<0){
                return 0;
            }
        }
    }


        return result;
    }

    @Override
    public List<ProjectListVo> list(int pageNum, int pageSize) {
        //返回一个声明值
        List<ProjectListVo> projectListVos=new ArrayList<>();
        //分页插件只会对它下面一个sql有效
        PageHelper.startPage(pageNum,pageSize);
        //根据ProjectExample来获取数据库中project数据
        ProjectExample projectExample=new ProjectExample();
        List<Project> projectList=projectMapper.selectByExample(projectExample);
        for (Project project:projectList){
            ProjectListVo projectListVo=new ProjectListVo();
            Long projectId=project.getId();
            //把project表里所有的数据添加到返回值里
            projectListVo.setProjectId(projectId);
            projectListVo.setProjectBudget(project.getProjectBudget());
            projectListVo.setProjectName(project.getProjiectName());
            projectListVo.setProjectDuration(project.getProjectDuration());
            projectListVo.setProjectIntroduction(project.getProjectIntroduction());
            projectListVo.setProjectShortIntro(project.getProjectShortIntro());
            projectListVo.setWorkType(project.getWorkType());
            projectListVo.setWorkExplain(project.getWorkExplain());
            //项目类型
            ProjectTypeRelationExample projectTypeRelationExample=new ProjectTypeRelationExample();
            projectTypeRelationExample.createCriteria().andIdEqualTo(projectId);
            List<ProjectType> projectTypes=new ArrayList<>();
            List<ProjectTypeRelation> projectTypeRelations=projectTypeRelationMapper.selectByExample(projectTypeRelationExample);
            for (ProjectTypeRelation projectTypeRelation:projectTypeRelations){
               ProjectType projectType=projectTypeMapper.selectByPrimaryKey(projectTypeRelation.getProjectTypeId());
                projectTypes.add(projectType);
            }projectListVo.setProjectTypeIdList(projectTypes);
            //职位类型
            ProjectPositionTypeRelationExample projectPositionTypeRelationExample=new ProjectPositionTypeRelationExample();
            projectPositionTypeRelationExample.createCriteria().andIdEqualTo(projectId);
            List<PositionType> positionTypes=new ArrayList<>();
            List<ProjectPositionTypeRelation> projectPositionTypeRelations=projectPositionTypeRelationMapper.selectByExample(projectPositionTypeRelationExample);
            for (ProjectPositionTypeRelation projectPositionTypeRelation:projectPositionTypeRelations){
                PositionType positionType=positionTypeMapper.selectByPrimaryKey(projectPositionTypeRelation.getPostitionTypeId());
               /*PositionTypeExample positionTypeExample=new PositionTypeExample();
                positionTypeExample.createCriteria().andIdEqualTo(projectPositionTypeRelation.getPostitionTypeId());
                List<PositionType> positionTypes=positionTypeMapper.selectByExample(positionTypeExample);*/
                /*projectListVo.setEngineerTypeList(positionType);*/
                positionTypes.add(positionType);
            }
            projectListVo.setEngineerTypeList(positionTypes);
            //项目关联文件
            ProjectFilesExample projectFilesExample=new ProjectFilesExample();
            projectFilesExample.createCriteria().andIdEqualTo(projectId);
            List<ProjectFiles> projectFiles=projectFilesMapper.selectByExample(projectFilesExample);
            projectListVo.setFilesList(projectFiles);
            //项目进度
            ProjectScheduleRelationExample projectScheduleRelationExample=new ProjectScheduleRelationExample();
            projectScheduleRelationExample.createCriteria().andIdEqualTo(projectId);
            List<Schedule> schedules=new ArrayList<>();
            List<ProjectScheduleRelation> projectScheduleRelations=projectScheduleRelationMapper.selectByExample(projectScheduleRelationExample);
            for (ProjectScheduleRelation projectScheduleRelation:projectScheduleRelations){
            Schedule schedule=scheduleMapper.selectByPrimaryKey(projectScheduleRelation.getScheduleId());
            schedules.add(schedule);
            }
            projectListVo.setProjectSchedule(schedules);
           projectListVos.add(projectListVo);
        }

        return projectListVos;
    }
}
